/*******************************************************************************
 * Copyright (c) 2010, 2030 www.mahao.cn
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 *******************************************************************************/
package cn.itlaobing.eshopmanagement.entity.typehandlers;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;

/** 
 * ClassName: StringArrayTypeHandlers <br/> 
 * Function: TODO 功能描述 <br/> 
 * date: 2017年5月18日 下午5:09:57 <br/> 
 * 
 * @author Mmmmm 
 * @version  
 * @since JDK 1.8 
 */
public class StringArrayTypeHandlers extends BaseTypeHandler<String []>{

	@Override
	public void setNonNullParameter(PreparedStatement ps, int i, String[] parameter, JdbcType jdbcType)
			throws SQLException {
		ps.setString(i, StringUtils.join(parameter,","));
	}

	@Override
	public String[] getNullableResult(ResultSet rs, String columnName) throws SQLException {
		String result=rs.getString(columnName);
		if(StringUtils.isNoneBlank(result)){
			return result.split(",");
		}
		return null;
	}

	/* (non-Javadoc)
	 * @see org.apache.ibatis.type.BaseTypeHandler#getNullableResult(java.sql.ResultSet, int)
	 */
	@Override
	public String[] getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
		String result=rs.getString(columnIndex);
		if(StringUtils.isNoneBlank(result)){
			return result.split(",");
		}
		return null;
	}

	/* (non-Javadoc)
	 * @see org.apache.ibatis.type.BaseTypeHandler#getNullableResult(java.sql.CallableStatement, int)
	 */
	@Override
	public String[] getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
		String result=cs.getString(columnIndex);
		if(StringUtils.isNoneBlank(result)){
			return result.split(",");
		}
		return null;
	}

}
